<?php
namespace Home\Controller;
use Common\Controller\CommonController;
/**
 * 附近人
 */
class NearbyPeopleController extends CommonController
{
    public $model;
    
    public function _initialize(){ 
        parent::_initialize();
        $this->model = M('user');
    }

    public function index(){
        $map = ['is_nearby'=>1];
        list($list, $page) = $this->lists($this->model, $map, 'id desc');
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->display();
    }

    public function add()
    {
        $id = I('id');
        if (IS_POST) {
            $data = I('post.');
            if(empty($data['id'])){
                $this->ajaxReturn(['s'=>false, 'm'=>'请选择用户']);
            }
            $data['is_nearby'] = 1;
            $data['update_time'] = time();
            list($pro, $city, $area) = explode('/', $data['city']);
            $data['nearby_pro'] = $pro;
            $data['nearby_city'] = $city;
            $data['nearby_area'] = $area;
            unset($data['city']);
            $nearby_img = '';
            if($data['nearby_imgs']){
                $nearby_imgs = explode(',', $data['nearby_imgs']);
                if($nearby_imgs){
                    $nearby_img = $nearby_imgs[0];
                }
            }
            $data['nearby_img'] = $nearby_img;
            if($this->model->create($data)){
                $res = $this->model->where(['id'=>$id])->save();
                if($res !== false){
                    $this->ajaxReturn(['s'=>true, 'm'=>'新增成功', 'url' => U('index')]);
                }else{
                    $this->ajaxReturn(['s'=>false, 'm'=>'新增失败']);
                }
            }else{
                $this->ajaxReturn(['s'=>false, 'm'=>$this->model->getError()]);
            }
        } else {
            $users = M('user')->where(['is_nearby'=>0])->field('id,nickname')->select();
            $this->assign('users', $users);
            $this->display();
        }
    }

    public function edit()
    {
        $id = I('id');
        if (IS_POST) {
            $data = I('post.');
            $data['update_time'] = time();
            list($pro, $city, $area) = explode('/', $data['city']);
            $data['nearby_pro'] = $pro;
            $data['nearby_city'] = $city;
            $data['nearby_area'] = $area;
            unset($data['city']);
            $nearby_img = '';
            if($data['nearby_imgs']){
                $nearby_imgs = explode(',', $data['nearby_imgs']);
                if($nearby_imgs){
                    $nearby_img = $nearby_imgs[0];
                }
            }
            $data['nearby_img'] = $nearby_img;
            if($this->model->create($data)){
                $res = $this->model->where(['id'=>$id])->save();
                if($res !== false){
                    $this->ajaxReturn(['s'=>true, 'm'=>'修改成功', 'url' => U('index')]);
                }else{
                    $this->ajaxReturn(['s'=>false, 'm'=>'修改失败']);
                }
            }else{
                $this->ajaxReturn(['s'=>false, 'm'=>$this->model->getError()]);
            }
        } else {
            $info = $this->model->where(['id' => $id])->find();
            if(empty($info)){
                $this->error('信息不存在');
            }
            $info['city'] = implode('/', [$info['nearby_pro'],$info['nearby_city'],$info['nearby_area']]);
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function del(){
        $id = I('id');
        $res = $this->model->where(['id' => $id])->save(['is_nearby'=>0]);
        if ($res) {
            $this->ajaxReturn(array('s' => true, 'm' => '删除成功', 'url' => U('index')));
        } else {
            $this->ajaxReturn(array('s' => false, 'm' => '删除失败!'));
        }
    }
}